#!/usr/bin/php
<?PHP
// usage: thirdpartysum.php <customername> <dbserver> <watch directory> <archive directory> {debug}
// assumptions:
//    1. customername is the same as the schema name on dbserver
//

//Start the script timer
require("/var/www/html/matchtest/classes/class.phpmailer.php");

$currenttime = time();
$readabledate = date("D, M j H:i:s",$currenttime);
$filedate = date("YmdHis",$currenttime);

$time_start = getmicrotime();
// create the error.txt file on the server

$lines = file('/http/matchtest/testresults.txt');
$numitems = count($lines);

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
	$pieces = explode("|",$line);
//	print_r($pieces);
	if($line_num == 0){ $datestamp = trim($pieces[0]); }
	else {
		$status[$line_num] = $pieces[0];
		$testname[$line_num] = $pieces[1];
		$testdesc[$line_num] = $pieces[2];
		$testurl[$line_num] = $pieces[3];
		$testtime[$line_num] = trim($pieces[4]);
    	echo "Line #<b>{$line_num}</b> Status: " . $status[$line_num] . "<br />\n";
    	echo "Line #<b>{$line_num}</b> Test Name: " . $testname[$line_num] . "<br />\n";
    	echo "Line #<b>{$line_num}</b> Test Description: " . $testdesc[$line_num] . "<br />\n";
    	echo "Line #<b>{$line_num}</b> Test Time: " . $testurl[$line_num] . "<br />\n";
    	echo "Line #<b>{$line_num}</b> Test Time: " . $testtime[$line_num] . "<br />\n";
	}
}

	$i = 1;
	$emailbody = "<font face=\"Tahoma\" size=\"2\">\n";
	$emailbody = $emailbody."<table class=\"record-list\">\n";
	$emailbody = $emailbody."<thead>\n";
    $emailbody = $emailbody."<tr>\n";
    $emailbody = $emailbody."<th>Status</th>\n";
    $emailbody = $emailbody."<th>Test Name</th>\n";
    $emailbody = $emailbody."<th>Description</th>\n";
    $emailbody = $emailbody."<th>Test Time</th>\n";
    $emailbody = $emailbody."<th>&nbsp;</th>\n";
    $emailbody = $emailbody."</tr>\n";
    $emailbody = $emailbody."</thead>\n";
    $emailbody = $emailbody."<tbody>\n";

	$EmailStatus = "PASSED";
	while ($i < $numitems){
		$emailbody = $emailbody."<tr>\n";
		$emailbody = $emailbody."<td>".ucfirst($status[$i])."</td>\n";
		$emailbody = $emailbody."<td><a href=\"http://tests.aimatch.com/tests/".$testurl[$i]."?web=1\" target=_blank>".$testname[$i]."</a></td>\n";
		$emailbody = $emailbody."<td>".$testdesc[$i]."</td>\n";
		$emailbody = $emailbody."<td>".date("n/j/Y g:ia",$testtime[$i])."</td>\n";
		$emailbody = $emailbody."<td class=\"action\"></td>\n";
		$emailbody = $emailbody."</tr>\n";
		if($status[$i] == "fail"){
			$EmailStatus = "FAILED";}
	$i++;
	}
    $emailbody = $emailbody."</tbody>\n";
    $emailbody = $emailbody."</table>\n";

    $emailbody = $emailbody."</font>\n";
	
	echo $emailbody;
	
	    $mail = new phpmailer();

        $mail->IsSendmail();                                      // set mailer to use SMTP
        $mail->Host = "smtp.gmail.com";       
        $mail->From = "scriptserver@aimatch.com";
        $mail->FromName = "Canary Testing";
        $mail->SMTPAuth = "true";
        $mail->Username = "aimatchrobc@gmail.com";
        $mail->Password = "@1m@tchr0bc";
        $mail->AddAddress("rob@aimatch.com");
        $mail->AddAddress("dev@aimatch.com");
        $mail->AddAddress("steved@aimatch.com");
        $mail->AddReplyTo("rob@aimatch.com", "Robert Cox");

        $mail->WordWrap = 65;                                 // set word wrap to 65 characters
        $mail->IsHTML(true);                                  // set email format to HTML

        
        $mail->Subject = "CANARY: Canary Testing ".$EmailStatus." on ".$readabledate;
        $mail->Body    = $emailbody;

        if(!$mail->Send())
        {
            echo "Message could not be sent. <p>";
            echo "Mailer Error: " . $mail->ErrorInfo;
            exit;
         }

//
//Function: Gets the microtime from the system to be used in timing the execution of this script.
//          Returns the current time in microseconds. 
//
function getmicrotime(){
   list($usec, $sec) = explode(" ",microtime());
   return ((float)$usec + (float)$sec);
   }


?>
